package com.fosun.spr.plugin.learn.api.v2Tbshop.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.learn.api.v2Tbshop.dao.V2TbshopDao;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.V2TbshopService;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.DeleteSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.DeleteSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.GetSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.GetSrModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.ListSpModel;
import com.fosun.spr.plugin.learn.api.v2Tbshop.service.model.ListSrModel;
import com.fosun.spr.plugin.learn.helper.mybatis.dao.V2TbshopMapper;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2Tbshop;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2Tbshop;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2TbshopExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class V2TbshopServiceImpl extends CoBaseService implements
		V2TbshopService {
	@Autowired 
	private V2TbshopDao v2TbshopDao;
	@Autowired
	private V2TbshopMapper v2TbshopMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(v2TbshopDao.list(spModel));
		srModel.setTotalCount(v2TbshopDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		V2Tbshop db = v2TbshopMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"淘宝店铺管理-v2_tbshop");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		V2Tbshop db = v2TbshopMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		v2TbshopMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getTitle(),"店名");
		ExtAsserts.notNull(spModel.getTaobaoUrl(),"店铺链接");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		V2TbshopExample eTitle = new V2TbshopExample();
		V2TbshopExample.Criteria cTitle = eTitle.createCriteria();
		cTitle.andTitleEqualTo(spModel.getTitle());
		cTitle.andIsDeletedEqualTo(IS_DELETED_N);
		List<V2Tbshop> rowsTitle = v2TbshopMapper.selectByExample(eTitle);		
		
		//if(StringUtils.isBlank(spModel.getId())){
		if(spModel.getId()==null){
			if(rowsTitle.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}		
			
			V2Tbshop db = new V2Tbshop();
			cglibCopyProperties(spModel, db, true);
				db.setTbUrlTime(new Date());
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			//db.setId(getUuid());
			v2TbshopMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsTitle.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}			
			if(rowsTitle.size()==1 && !rowsTitle.get(0).getTitle().equals(spModel.getTitle())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getTitle()+" 已经存在");
			}	
			
			V2Tbshop db = v2TbshopMapper.selectByPrimaryKey(spModel.getId());
			db.setTitle(spModel.getTitle());
			db.setTaobaoUrl(spModel.getTaobaoUrl());
			db.setInitFavorites(spModel.getInitFavorites());
			v2TbshopMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
